﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Interface;

namespace Unity2._1
{
    public class UnityHelper
    {
        /// <summary>
        /// 内部的底层容器。
        /// </summary>
        private static IUnityContainer _container = new UnityContainer();

        /// <summary>
        /// 注册单个组件
        /// </summary>
        /// <param name="containerName">子容器名称</param>
        /// <param name="key">组件标识</param>
        /// <param name="service">组件的接口</param>
        /// <param name="type">组件的类名</param>
        public static void AddComponent(string containerName, string key, string service, string type)
        {
            IUnityContainer container = null;
            try
            {
                container = _container;
            }
            catch
            {

            }

            if (container == null)
            {
                UnityConfigurationSection section = (UnityConfigurationSection)
                    System.Configuration.ConfigurationManager.GetSection("unity");
                section.Configure(container, containerName);
            }
            //if (container.Kernel.HasComponent(key) == false)
            //{
            //    container.AddComponent(key, Type.GetType(service), Type.GetType(type));
            //}
        }

        /// <summary>
        /// 通过Unity实组装对象泛型方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static T UnityToT<T>(string name)
        {
            IUnityContainer container = CreateContainer();
            T model = container.Resolve<T>(name);
            return model;
        }
        /// <summary>
        /// 通过Unity实组装对象泛型方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static T UnityToT<T>()
        {
            IUnityContainer container = CreateContainer();
            T model = container.Resolve<T>();
            return model;
        }
        /// <summary>
        /// 通过配置创建UnityContainer
        /// </summary>
        /// <returns></returns>
        public static IUnityContainer CreateContainer()
        {
            IUnityContainer container = new UnityContainer();
            UnityConfigurationSection section = (UnityConfigurationSection)
                System.Configuration.ConfigurationManager.GetSection("unity");
            //section.Containers["Guestbook"].Configure(container);
            section.Configure(container, "Guestbook");
            return container;
        }


        public static object GetInstance(string containerName, Type T)
        {
            IUnityContainer container = new UnityContainer();
            UnityConfigurationSection section = (UnityConfigurationSection)
                System.Configuration.ConfigurationManager.GetSection("unity");
            section.Configure(container, containerName);

            object instance = null;

            try
            {
                instance = container.Resolve<T>();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return instance;
        }
    }
}
